package com.shanjupay.merchant.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.common.util.PhoneUtil;
import com.shanjupay.merchant.common.util.SecurityUtil;
import com.shanjupay.merchant.convert.MerchantDetailConvert;
import com.shanjupay.merchant.convert.MerchantRegisterConvert;
import com.shanjupay.merchant.feign.MerchantFeign;
import com.shanjupay.merchant.service.FileService;
import com.shanjupay.merchant.service.SmsService;
import com.shanjupay.merchant.vo.MerchantDetailVO;
import com.shanjupay.merchant.vo.MerchantRegisterVO;
import com.shanjupay.service.api.MerchantService;
import com.shanjupay.service.api.dto.MerchantDTO;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.BatchUpdateException;
import java.util.UUID;

/**
 * TODO
 *
 * @Description
 * @Author zhangbing
 * @Date 2024/5/8 8:47
 **/
@RestController  //controller+responsebody
public class MerchantController {

    @Autowired
    private MerchantFeign merchantFeign; //外部引用

    @Autowired
    private SmsService service; //内部调用

    @Autowired
    private FileService fileService;

    @GetMapping("/{id}") //加了配置文件中的路径 /merchant/id
    public MerchantDTO findById(@PathVariable("id") Long id){
        MerchantDTO merchantDTO = merchantFeign.findById(id);
        return merchantDTO;
    }

    //发送验证码
    @GetMapping("/sms")
    public String getSMSCode(@RequestParam String phone){

        return  service.SendMsg(phone);
    }
    //注册商户
    @PostMapping("/register")
    public MerchantRegisterVO registerMerchant(@RequestBody MerchantRegisterVO merchantRegisterVO){
        if(merchantRegisterVO ==null){
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        //是否为null、是否为""、是否为空字符串(引号中间有空格)" "、制表符、换行符、换页符和回车
        if(StringUtils.isBlank(merchantRegisterVO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100112);
        }
        //手机号格式校验
        if(!PhoneUtil.isMatches(merchantRegisterVO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100109);//手机号格式不正确
        }
        //校验验证码
        service.checkVerifiyCode(merchantRegisterVO.getVerifiykey(),merchantRegisterVO.getVerifiyCode());

        //entity to  dto 转换
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.entity2dto(merchantRegisterVO);
            merchantDTO.setPassword(merchantRegisterVO.getPassword());
        //把数据传到Service进行注册
        MerchantDTO merchant = merchantFeign.createMerchant(merchantDTO);
        return  merchantRegisterVO;
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException, BatchUpdateException {
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        //后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") - 1);
        //文件名称
        String fileName=UUID.randomUUID().toString()+suffix;
        //上传
        String fileUrl = fileService.upload(file.getBytes(), fileName);
        return fileUrl;
    }

    /**
     * 商户资质申请
     */
    @PostMapping("/applyMerchant")
    public void saveMerchant(@RequestBody MerchantDetailVO merchantDetailVO){
        //解析id token 取出当前id
        Long merchantId = SecurityUtil.getMerchantId();
        System.out.println(merchantId);
        MerchantDTO merchantDTO = MerchantDetailConvert.INSTANCE.vo2dto(merchantDetailVO);
        merchantDTO.setId(merchantId);
        System.out.println("merchantDTO:"+merchantDTO);
        merchantFeign.applyMerchant(merchantDTO);
    }



}
